﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Husb.Util;
using Husb.Util.Enum;
using CentralOffice.Task.Data;
using CentralOffice.Task.BusinessActions;

using ApplicationTable = CentralOffice.Task.Data.ApplicationDataSet.ApplicationsDataTable;
using ApplicationRow = CentralOffice.Task.Data.ApplicationDataSet.ApplicationsRow;

using ApprovalTable = CentralOffice.Task.Data.ApprovalDataSet.ApprovalsDataTable;
using ApprovalRow = CentralOffice.Task.Data.ApprovalDataSet.ApprovalsRow;
using System.Data;

namespace CentralOffice.WebClient.Tasks
{
    public partial class ApplicationExecutorUpdate : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string applicationId = HttpContext.Current.Request.QueryString["id"];
                ApplicationRow application = CentralOffice.Task.BusinessActions.Applications.GetById(new Guid(applicationId));

                lblApplicationCode.Text = application.IsApplicationCodeNull() ? "" : application.ApplicationCode;
                lblApplicant.Text = application.Applicant;
                lblApprovalContent.Text = application.ApprovalContent;
                lblDateOfApplication.Text = application.DateOfApplication.ToString();
                lblName.Text = application.Name;
                lblStatus.Text = EnumUtil.GetDisplayName(SiteConstants.ApprovalStatus, CategoryDataSourceType.Xml, application.Status);

                //lblApplicationExecutor.Text = application.IsApplicationExecutorNull() ? "" : application.ApplicationExecutor;
                lblExecutiveDescription.Text = application.IsExecutiveDescriptionNull() ? "" : application.ExecutiveDescription;

                if (!application.IsApplicationExecutorNull())
                {
                    lblApplicationExecutor.Text = application.ApplicationExecutor;
                    txtExecutor.Value = application.ApplicationExecutor;
                    txtExecutorReadOnly.Value = application.ApplicationExecutor;
                }

                if (!application.IsAttachmentsNull() && application.Attachments.Length > 0)
                {
                    lblAttachments.Visible = true;
                    SiteUtil.GenernateDownloadHyperLink(lblAttachments, application.Attachments);
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string applicationId = HttpContext.Current.Request.QueryString["id"];
            ApplicationRow application = CentralOffice.Task.BusinessActions.Applications.GetById(new Guid(applicationId));
            ApplicationDataSet applicationDataSet = application.Table.DataSet as ApplicationDataSet;

            application.ApplicationExecutor = txtExecutor.Value;
            CentralOffice.Task.BusinessActions.Applications.Update(application.Table.DataSet);

            Response.Redirect("ApplicationExecutor.aspx");

        }

        protected void btnShowPopup_Click(object sender, EventArgs e)
        {
            string[] assignedBy = null; //txtApprovedBy.Value.Split(',');
            LinkButton btn = sender as LinkButton;
            if (btn != null)
            {
                assignedBy = txtExecutor.Value.Split(',');
            }

            tvEmp.Nodes.Clear();
            //HumanResources.HumanResourcesHelper.PopulateEmployeeTree(tvEmp, true, assignedBy);
            HumanResources.HumanResourcesHelper.PopulateEmployeeTree(tvEmp, true, assignedBy);//null

            updPnlDepartment.Update();
            mdlDetailView.Show();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            // 保存弹出窗口所更新的信息
            string employees = "";
            string employeesId = "";

            if (tvEmp.CheckedNodes.Count > 0)
            {
                foreach (TreeNode node in tvEmp.CheckedNodes)
                {
                    if (!node.Text.StartsWith("["))
                    {
                        employees += node.Text + ",";
                        employeesId += node.Value + ",";
                    }
                }
            }

            txtExecutorReadOnly.Value = employees.Trim(',');
            txtExecutor.Value = employees.Trim(',');
        }

        protected void tvEmp_Load(object sender, EventArgs e)
        {
            this.tvEmp.Attributes.Add("OnClick", "OnTreeNodeChecked(event)");
        }

    }
}
